Importing libraries for reading and displaying images¶

In [1]:
#for reading images
import cv2
#for displaying images
import matplotlib.pyplot as plt

Reading image¶

In [2]:
image = cv2.imread('sample.jpg')
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
img = img.tolist()

plt.imshow(img)
plt.show()

Displaying Image Comparisons¶

In [3]:
def compare(original, manipulated, title_1="Original", title_2="Manipulated"):
    plt.figure(figsize=(15, 25))
    plt.subplot(1, 2, 1)
    plt.title(title_2)
    plt.imshow(manipulated)
    plt.subplot(1, 2, 2)
    plt.title(title_1)
    plt.imshow(original)
    plt.show()

User defined Functions

Here, we will code some functions which will help in complex manipulations further

In [4]:
def print_shape(img):
    try:
        print("Shape of the array is:", len(img), "x", len(img[0]), "x",
              len(img[0][0]))
    except:
        print("Shape of the array is:", len(img), "x", len(img[0]))


def add_list(img1, img2):
    return [[img1[i][j] + img2[i][j] for j in range(len(img1[0]))]
            for i in range(len(img1))]


#original shape
print("Original:")
print_shape(img)
print("\n")


def channel_first(img):
    return [[[img[j][k][i] for k in range(len(img[0]))]
             for j in range(len(img))] for i in range(len(img[0][0]))]


print("After Channel_first operation")
z = channel_first(img)
print_shape(z)
print("\n")


def channel_last(img):
    return [[[img[k][i][j] for k in range(len(img))]
             for j in range(len(img[0][0]))] for i in range(len(img[0]))]


print("After Channel_last operation")
z = channel_last(z)
print_shape(z)
Original:
Shape of the array is: 533 x 800 x 3


After Channel_first operation
Shape of the array is: 3 x 533 x 800


After Channel_last operation
Shape of the array is: 533 x 800 x 3

Displaying channels¶

In [5]:
channel_wise = channel_first(img)
plt.figure(figsize=(10, 20))
plt.subplot(1, 3, 1)
plt.title("Red")
plt.imshow(channel_wise[0], cmap="gray")
plt.subplot(1, 3, 2)
plt.title("Green")
plt.imshow(channel_wise[1], cmap="gray")
plt.subplot(1, 3, 3)
plt.title("Blue")
plt.imshow(channel_wise[2], cmap="gray")
plt.show()


Image Manipulation

Channel-wise addition¶

Adding all channel(RGB) values to a single channel

In [6]:
def channel_wise_addition(img):
    temp = channel_first(img)
    return add_list(temp[2], add_list(temp[0], temp[1]))


plt.imshow(channel_wise_addition(img), cmap="gray")
plt.show()

Invert¶

In [7]:
def invert(img):
    return [[[255 - k for k in j] for j in i] for i in img]


compare(img, invert(img))

Mirror Vertical¶

In [8]:
def mirror_v(img):
    return [img[-i - 1] for i in range(len(img))]


compare(img, mirror_v(img))

Mirror Horizontal¶

In [9]:
def mirror_h(img):
    return [[img[i][-j - 1] for j in range(len(img[0]))]
            for i in range(len(img))]


compare(img, mirror_h(img))

Rotate¶

In [10]:
#rotate left
def rotate_left(img):
    return [[[img[j][-1-i][k] for k in range(len(img[0][0]))]
             for j in range(len(img))] for i in range(len(img[0]))]


compare(img, rotate_left(img))


#rotate right
def rotate_right(img):
    return [[[img[-1 - j][i][k] for k in range(len(img[0][0]))]
             for j in range(len(img))] for i in range(len(img[0]))]


compare(img, rotate_right(img))

Pad¶

In [18]:
def pad(img, left_right_top_bottom, value=0):
    temp1 = img.copy()
    for i in range(left_right_top_bottom[2]):
        temp1.insert(0, [[value for i in range(len(temp1[0][0]))]
                         for i in range(len(temp1[0]))])
    for i in range(left_right_top_bottom[3]):
        temp1.insert(-1, [[value for i in range(len(temp1[0][0]))]
                          for i in range(len(temp1[0]))])
    temp1 = rotate_right(temp1)
    for i in range(left_right_top_bottom[0]):
        temp1.insert(0, [[value for i in range(len(temp1[0][0]))]
                         for i in range(len(temp1[0]))])
    for i in range(left_right_top_bottom[1]):
        temp1.insert(-1, [[value for i in range(len(temp1[0][0]))]
                          for i in range(len(temp1[0]))])
    temp1 = rotate_left(temp1)
    return temp1


compare(img,pad(img, (20, 40, 60, 100), 255))

Blur¶

Method:1

In [11]:
def blur_1(img, strength=1):
    temp1 = []
    for i in range(len(img)):
        temp2 = []
        for j in range(len(img[0])):
            temp3 = []
            for k in range(len(img[0][0])):
                temp4 = []
                for x in range(1, strength + 1):
                    a_pixels = 1
                    temp = img[i][j][k]
                    try:
                        temp += img[i + x][j + x][k]
                        a_pixels += 1
                    except:
                        True
                    try:
                        temp += img[i + x][j][k]
                        a_pixels += 1
                    except:
                        True
                    try:
                        temp += img[i + x][j - x][k]
                        a_pixels += 1
                    except:
                        True
                    try:
                        temp += img[i][j - x][k]
                        a_pixels += 1
                    except:
                        True

                    try:
                        temp += img[i - x][j - x][k]
                        a_pixels += 1
                    except:
                        True
                    try:
                        temp += img[i - x][j][k]
                        a_pixels += 1
                    except:
                        True
                    try:
                        temp += img[i - x][j + x][k]
                        a_pixels += 1
                    except:
                        True
                    try:
                        temp += img[i][j + x][k]
                        a_pixels += 1
                    except:
                        True
                    temp4.append(temp / a_pixels)
                temp3.append(int(sum(temp4) / len(temp4)))
            temp2.append(temp3)
        temp1.append(temp2)

    return temp1


compare(img, blur_1(img, strength=5))
compare(img, blur_1(img, strength=10))
compare(img, blur_1(img, strength=20))

Method:2

In [12]:
def blur_2(img, strength=1):

    def blur_strength_1(img):
        temp1 = []
        for i in range(len(img)):
            temp2 = []
            for j in range(len(img[0])):
                temp3 = []
                for k in range(len(img[0][0])):
                    a_pixels = 1
                    temp = img[i][j][k]
                    try:
                        temp += img[i + 1][j + 1][k]
                        a_pixels += 1
                    except:
                        True
                    try:
                        temp += img[i + 1][j][k]
                        a_pixels += 1
                    except:
                        True
                    try:
                        temp += img[i + 1][j - 1][k]
                        a_pixels += 1
                    except:
                        True
                    try:
                        temp += img[i][j - 1][k]
                        a_pixels += 1
                    except:
                        True

                    try:
                        temp += img[i - 1][j - 1][k]
                        a_pixels += 1
                    except:
                        True
                    try:
                        temp += img[i - 1][j][k]
                        a_pixels += 1
                    except:
                        True
                    try:
                        temp += img[i - 1][j + 1][k]
                        a_pixels += 1
                    except:
                        True
                    try:
                        temp += img[i][j + 1][k]
                        a_pixels += 1
                    except:
                        True

                    temp3.append(int(temp / a_pixels))
                temp2.append(temp3)
            temp1.append(temp2)
        return temp1

    temp = img.copy()
    for i in range(strength):
        temp = blur_strength_1(temp)
    return temp


compare(img, blur_2(img, 5))
compare(img, blur_2(img, 10))
compare(img, blur_2(img, 20))

Comparing two blurring methods¶

In [13]:
compare(blur_1(img, 15), blur_2(img, 15), "Blur_1", "Blur_2")

Resize¶

In [14]:
def resize(img, size):

    return [[[
        img[int(len(img) * i / size[0])][int(len(img[0]) * j / size[1])][k]
        for k in range(3)
    ] for j in range(size[1])] for i in range(size[0])]


compare(img, resize(img, (500, 500)))

Lightness¶

In [15]:
def lightness(img, b=50):

    return [[[
        int((255 * (b / 100)) + (img[i][j][k] * (1 - (b / 100))))
        for k in range(len(img[0][0]))
    ] for j in range(len(img[0]))] for i in range(len(img))]


compare(img, lightness(img, 5))
compare(img, lightness(img, 10))
compare(img, lightness(img, 50))

Brightness¶

In [16]:
def brightness(img, strength=0):
    return [[[
        int((510 / (1 + (2.7183**(-strength * img[i][j][k] / 255)))) - 255)
        for k in range(len(img[0][0]))
    ] for j in range(len(img[0]))] for i in range(len(img))]


compare(img, brightness(img, 2))
compare(img, brightness(img, 3))
compare(img, brightness(img, 5))

Contrast¶

In [17]:
def contrast(img, strength=0):
    return [[[
        int(255 / (1 + (2.7183**(-strength *
                                 ((img[i][j][k] - 127.5) / 127.5)))))
        for k in range(len(img[0][0]))
    ] for j in range(len(img[0]))] for i in range(len(img))]


compare(img, contrast(img, 1))
compare(img, contrast(img, 2))
compare(img, contrast(img, 5))
compare(img, contrast(img, 10))